<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Gregwar\Captcha\CaptchaBuilder;

use Session;
class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    //设置验证成功后的路径
    protected $redirectPath = '/blog';
    //设置验证失败的路径
    protected $loginPath = '/auth/login';
    //设置验证的字段
    protected $username = 'name';
    //设置退出后的路径
    protected $redirectAfterLogout = '/auth/login';

    
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {

        //自定义错误信息
        $messages = [
            'required' =>  ":attribute 必须填写",
            'max'     =>  ":attribute 格式不正确",
            'min'     =>  ":attribute 最少6位",
            'size'    =>  ":attribute 位数不对",
        ];

        //验证规则
        return Validator::make($data, [
            'name'      => 'required|max:255',
            'email'     => 'required|email|max:255|unique:users',
            'password'  => 'required|confirmed|min:6',
            'phone'     => 'required|size:11',
        ],$messages);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name'     => $data['name'],
            'email'    => $data['email'],
            'phone'    => $data['phone'],
            'password' => bcrypt($data['password']),
        ]);
    }

    /**
    *渲染登陆页面
    *@param
    *@return auth.login
    */
   /* public function getLogin()
    {
        return view('auth.login');
    }
*/
    /**
    *渲染注册页面
    *@param
    *@return auth.register
    */
  /*  public function getRegister()
    {

        return view('auth.register');   
    }*/

    //获取验证码
    public function code($tmp)
    {

         //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 150, $height = 60, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        Session::flash('milkcaptcha', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
    }
    


}

